package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.util.InvokeUtil;
import cn.e4j.modular.crm.entity.GwboT;
import cn.e4j.modular.crm.mapper.GwboTMapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 用户服务区域维护 服务实现类
 *
 * @author yuan_rz
 * @since 2019-01-5
 */
@Service
public class Gwai302ServiceImpl extends ServiceImpl<GwboTMapper, GwboT>{

    @Transactional(readOnly = true)
    public List<Dto> getListData(Page<Object> page, Dto dto) {
        List<Dto> listData = this.baseMapper.getListData(page, dto);
        return listData;
    }

    @Transactional(readOnly = true)
    public List<Dto> getUsersList(Page<Object> page, Dto dto) {
        List<Dto> listData = this.baseMapper.getUsersList(page, dto);
        return listData;
    }

    @Transactional
    public void addData(GwboT gwboT){
        Dto dto = Dtos.newDto();
        dto.put("ENT",gwboT.getGWBOENT());
        dto.put("GWBO001",gwboT.getGWBO001());
//        gwboT.setGWBO002(this.baseMapper.getGwboSeq(dto));
        gwboT.setGWBO002(InvokeUtil.invokeBeanMethod("gwboTMapper,getGwboSeq",dto));
        gwboT.insert();
    }

    @Transactional
    public void editData(GwboT gwboT){
        //修改1
        UpdateWrapper<GwboT> gwboTUpdateWrapper = new UpdateWrapper<>();
        gwboTUpdateWrapper.eq("GWBOENT",gwboT.getGWBOENT());
        gwboTUpdateWrapper.eq("GWBO001",gwboT.getGWBO001());
        gwboTUpdateWrapper.eq("GWBO002",gwboT.getGWBO002());

        gwboT.update(gwboTUpdateWrapper);
    }

    @Transactional
    public void deleteData(GwboT gwboT){
        delete(gwboT);
    }

    @Transactional
    public void deleteDataBatch(List<GwboT> gwboTList){
        for(int i=0; i < gwboTList.size(); i++){
            delete(gwboTList.get(i));
        }
    }

    private void delete(GwboT gwboT){
        //修改1
        UpdateWrapper<GwboT> gwboTUpdateWrapper = new UpdateWrapper<>();
        gwboTUpdateWrapper.eq("GWBOENT",gwboT.getGWBOENT());
        gwboTUpdateWrapper.eq("GWBO001",gwboT.getGWBO001());
        gwboTUpdateWrapper.eq("GWBO002",gwboT.getGWBO002());

        gwboT.delete(gwboTUpdateWrapper);
    }


}
